package org.xin.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.xin.common.result.ResultCode;
import org.xin.common.util.XinUtil;
import org.xin.framework.config.exception.CloudException;
import org.xin.security.pojo.bo.SecurityUser;
import org.xin.security.component.TokenComponent;
import org.xin.security.pojo.entity.SysUser;
import org.xin.security.mapper.SysUserMapper;
import org.xin.security.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import org.xin.framework.base.service.impl.IBaseServiceImpl;

import javax.servlet.http.HttpServletRequest;

/**
* <p>
    * 系统模块-用户表 服务实现类
    * </p>
*
* @author: xin
* @date: Create in 2021-09-29
*/
@Slf4j
@Service
public class SysUserServiceImpl extends IBaseServiceImpl<SysUser, SysUserMapper> implements ISysUserService {
    @Autowired
    private TokenComponent tokenComponent;

    @Override
    public SecurityUser loginUserInfo(HttpServletRequest request) {
        SecurityUser securityUser = tokenComponent.getSecurityUser(request);
        if (XinUtil.isNull(securityUser)) {
            throw new CloudException(ResultCode.UN_LOGIN);
        }
        return securityUser;
    }

    @Override
    public Boolean checkUserEmailUnique(String email) {
        lambdaQueryWrapper().eq(SysUser::getEmail, email);
        return baseMapper.selectCount(lambdaQueryWrapper) != 0;
    }

    @Override
    public Boolean checkUserPhoneUnique(String phone) {
        lambdaQueryWrapper().eq(SysUser::getPhone, phone);
        return baseMapper.selectCount(lambdaQueryWrapper) != 0;
    }
}
